﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Goods;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Goods
{
    public class GetGoodsHandler : IRequestHandler<GetGoodsCommand, ApiResult<List<GoodsDTO>>>
    {
        private readonly IBase<GoodsModel> goodsRepo;

        public GetGoodsHandler(IBase<GoodsModel> goodsRepo)
        {
            this.goodsRepo = goodsRepo;
        }

        public Task<ApiResult<List<GoodsDTO>>> Handle(GetGoodsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GoodsDTO>> result = new ApiResult<List<GoodsDTO>>();
            var goods=goodsRepo.GetAll();
            var linq = from g in goods
                       select new GoodsDTO
                       {
                           GoodsId = g.GoodsId,
                           GoodsName = g.GoodsName,
                       };
            result.Code = ApiEnum.OK;
            result.Data=linq.ToList();
            result.Msg = "商品显示成功";
            return Task.FromResult(result);
        }
    }
}
